<?php

namespace App\Store\Controllers\Message;

use App\Admin\Models\SiteCompany;
use App\Http\Controllers\Controller;
use App\Store\Models\Message\Phone;
use App\Store\Services\Message\Lianlu;
use Illuminate\Http\Request;
use App\Store\Validate\Message\Phone as ValidateMessagePhone;
use App\Common\Services\Store\Common as StoreCommon;
use Illuminate\Support\Facades\Log;

class PhoneController extends Controller
{
    public function dopostadd(Request $request)
    {
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];
        $req_data = $request->only(['title', 'phone_list', 'content']);
        $Validate = new ValidateMessagePhone('add');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        if (count(explode(',', $req_data['phone_list'])) >= 10000) {
            return response()->json(['code' => '001', 'msg' => '手机号最多只能填写1万个']);
        }

        $req_data['create_at'] = time();
        $req_data['cp_id'] =$cp_id;
        Phone::create($req_data);
        return response()->json(['code' => '000', 'msg' => '添加成功']);
    }

    public function getlist(Request $request)
    {
        $list = Phone::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    public function dopostedit(Request $request)
    {
        $req_data = $request->only(['id','phone_list', 'content']);
        $Validate = new ValidateMessagePhone('edit');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }
        if (count(explode(',', $req_data['phone_list'])) >= 10000) {
            return response()->json(['code' => '001', 'msg' => '手机号最多只能填写1万个']);
        }
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];

        $info = Phone::where(['id' => $req_data['id'],'cp_id'=>$cp_id])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }

        if($info->status!=1){
            return response()->json(['code' => '001', 'msg' => '短信已发送,不可修改']);
        }


        Phone::where('id', $req_data['id'])->update([
            'content' => $req_data['content'],
            'phone_list'=>$req_data['phone_list'],
            'update_at' => time(),
        ]);
        return response()->json(['code' => '000', 'msg' => '编辑成功']);
    }

    public function dodel(Request $request)
    {
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];
        $req_data = $request->only(['id']);
        $Validate = new ValidateMessagePhone('del');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $info = Phone::where(['id' => $req_data['id'],'cp_id'=>$cp_id])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }

        if($info->status!=1){
            return response()->json(['code' => '001', 'msg' => '短信已发送,不可删除']);
        }

        Phone::where('id', $req_data['id'])->update([
            'is_del' => 2,
            'update_at' => time(),
        ]);
        return response()->json(['code' => '000', 'msg' => '删除成功']);
    }

    public function send(Request $request)
    {
        $req_data = $request->only(['id']);
        $Validate = new ValidateMessagePhone('send');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];

        $one = Phone::where(['id' => $req_data['id'],'cp_id'=>$cp_id,'is_del'=>1])->first();
//        if (!$one || $one['status'] !== 1) {
//            return response()->json(['code' => '001', 'msg' => '发送失败，短信已发送']);
//        }

        if (empty($one)) {
            return response()->json(['code' => '001', 'msg' => '未获取活动模板']);
        }
        $one =$one->toArray();

        $phone_arr = array_unique(explode(',', $one['phone_list']));
        $content = $one['content'];

        $companyinfo = SiteCompany::where([['id',$cp_id],['is_open_sms',2]])->first();
        if(!$companyinfo){
            return response()->json(['code' => '001', 'msg' => '短信未开启']);
        }

        $res = Lianlu::NormalSend($phone_arr, $content, $companyinfo);
        Log::info('短信通知结果' . $res);
        $res = (array)json_decode($res, true);

        if (isset($res['message']) && $res['message'] == 'success') {
            Phone::where('id', $req_data['id'])->update([
                'status' => 3,
                'update_at' => time(),
            ]);
            return response()->json(['code' => '000', 'msg' => '发送成功']);
        }else{
            return response()->json(['code' => '001', 'msg' => '发送失败,'.$res['message']??'']);
        }

    }
}
